<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="anx")
 */
class Anx extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Anx();
        
    }

    
      /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $v){
                //分类
                $anx_type = Db::name("anx_type")->where("id",$v["anx_type_id"])->find();
                if(!empty($anx_type)){
                    $v['anx_type_name'] = $anx_type['title'];
                }else{
                    $v['anx_type_name'] = "";
                }
                $list[$k] = $v;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 


    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('标题不能为空');
            }
            if(empty($post['anx_type_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['img'])){
                $this->error('图片不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'img'=>$post['img'],
                'anx_type_id'=>$post['anx_type_id'],
                'sub_title'=>$post['sub_title'],
                'content'=>$post['content'],
                'create_time'=>time()
            ];
            try {
               Db::name("anx")->insert($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            // $publicKey64 = '';
            // $publicKeyMultiLine = chunk_split($publicKey64, 64, "\n");
            // dump($publicKeyMultiLine);die;
            $anx_type = Db::name("anx_type")->order("id asc")->select();
            $this->assign("anx_type",$anx_type);
            return $this->fetch();
        }
 
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('标题不能为空');
            }
            if(empty($post['anx_type_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['img'])){
                $this->error('图片不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'img'=>$post['img'],
                'anx_type_id'=>$post['anx_type_id'],
                'sub_title'=>$post['sub_title'],
                'content'=>$post['content']
            ];
            try {
                Db::name("anx")->where('id',$post['id'])->update($data);
           
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $row['content'] = html_entity_decode($row['content']);
            $row['content'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $row['content']);
            $this->assign('row', $row); 
            $anx_type = Db::name("anx_type")->order("id asc")->select();
            $this->assign("anx_type",$anx_type);
            return $this->fetch();
        }
       
    }



    
}